package cn.stylefeng.guns.modular.make.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.make.entity.MakeRailwayPlatformManage;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformManageParam;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformParam;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformManageResult;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformResult;
import cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformManageService;
import cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformService;
import cn.stylefeng.guns.modular.task.model.params.TaskOrderParam;
import cn.stylefeng.guns.modular.task.model.result.TaskOrderResult;
import cn.stylefeng.guns.modular.task.service.TaskOrderService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

/**
 * 月台预约管理控制器
 */
@Controller
@RequestMapping("/makeRailwayPlatformManage")
public class MakeRailwayPlatformManageController extends BaseController {

    private String PREFIX = "/make/makeRailwayPlatformManage";

    @Autowired
    private MakeRailwayPlatformManageService makeRailwayPlatformManageService;

    @Autowired
    private MakeRailwayPlatformService makeRailwayPlatformService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/makeRailwayPlatformManage.html";
    }

    /**
     * 跳转到车辆排队面板
     */
    @RequestMapping("makepanel")
    public String makepanel() {
        return PREFIX + "/makepanel.html";
    }

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    /**
     * 跳转到选择车辆页面
     */
    @RequestMapping("toChoiceCar")
    public String toChoiceCar(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));

    	//查询可选择承运商
    	BasicCarrierParam basicCarrierParam = new BasicCarrierParam();
    	basicCarrierParam.setDeliverId(deliverId);
    	model.addAttribute("carrierlist", basicCarrierService.findListBySpec(basicCarrierParam,user.getOperateId()));
        return PREFIX + "/choiceCar.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	User user = userService.getcurrentUser();
    	makeRailwayPlatformParam.setDeptId(user.getDeptId());
    	List<MakeRailwayPlatformResult> plist = makeRailwayPlatformService.findListBySpec(makeRailwayPlatformParam,user.getOperateId());
    	model.addAttribute("plist", plist);
        return PREFIX + "/makeRailwayPlatformManage_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	User user = userService.getcurrentUser();
    	makeRailwayPlatformParam.setDeptId(user.getDeptId());
    	List<MakeRailwayPlatformResult> plist = makeRailwayPlatformService.findListBySpec(makeRailwayPlatformParam,user.getOperateId());
    	model.addAttribute("plist", plist);
        return PREFIX + "/makeRailwayPlatformManage_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	User user = userService.getcurrentUser();
    	BasicOperate basicOperate = basicOperateService.getById(user.getOperateId());
    	return this.makeRailwayPlatformManageService.addMake(makeRailwayPlatformManageParam, user,basicOperate);
    }
    
    @Autowired
    private TaskOrderService taskOrderService;

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	User user = userService.getcurrentUser();
    	return this.makeRailwayPlatformManageService.editMake(makeRailwayPlatformManageParam,user);
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	makeRailwayPlatformManageParam.setState("已拒绝");
        this.makeRailwayPlatformManageService.update(makeRailwayPlatformManageParam);
        
        TaskOrderParam taskOrderParam = new TaskOrderParam();
        taskOrderParam.setMakeId(Long.parseLong(makeRailwayPlatformManageParam.getId().toString()));
        TaskOrderResult orderResult = taskOrderService.findBySpec(taskOrderParam);
        taskOrderParam.setId(orderResult.getId());
        taskOrderService.delete(taskOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
        MakeRailwayPlatformManage detail = this.makeRailwayPlatformManageService.getById(makeRailwayPlatformManageParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam,String condition ) {
    	User user = userService.getcurrentUser();
    	makeRailwayPlatformManageParam.setDeptId(user.getDeptId());
    	if (makeRailwayPlatformManageParam.getArriveTime() == null){
    		makeRailwayPlatformManageParam.setArriveTime(DateUtil.getDate());
		}
        return this.makeRailwayPlatformManageService.findPageBySpec(makeRailwayPlatformManageParam,condition,user.getOperateId());
    }
    
    /**
     * 查询大屏列表
     */
    @RequestMapping("/findDaping")
    @ResponseBody
    public ResponseData findDaping(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	User user = userService.getcurrentUser();
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	makeRailwayPlatformParam.setDeptId(user.getDeptId());
    	List<MakeRailwayPlatformResult> plist = makeRailwayPlatformService.findListBySpec(makeRailwayPlatformParam,user.getOperateId());
    	for (MakeRailwayPlatformResult r : plist) {
    		makeRailwayPlatformManageParam.setRailwayPlatformId(Long.parseLong(r.getId().toString()));
    		MakeRailwayPlatformManageResult jinxingzhong = makeRailwayPlatformManageService.findJingxingzhongBySpec(makeRailwayPlatformManageParam);
    		
    		MakeRailwayPlatformManageResult paiduizhong = makeRailwayPlatformManageService.findPaiduizhongBySpec(makeRailwayPlatformManageParam);
    		
    		r.setJinxingzhong(jinxingzhong);
    		r.setPaiduizhong(paiduizhong);
		}
    	return ResponseData.success(plist);
    }

}


